﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using OnlineVideos.Data;
using Common.Library;
using System.ComponentModel;
using Common.Utilities;
using Microsoft.Phone.Controls;
using OnlineVideos.UI;
using OnlineVideos.Entities;

namespace OnlineVideos
{
    public partial class FavoriteImages : UserControl
    {
        #region GlobalDeclaration

        List<ShowLinks> showFavImages = null;
        private bool IsDataLoaded;
        #endregion

        public FavoriteImages()
        {
            InitializeComponent();
            IsDataLoaded = false;
            showFavImages = new List<ShowLinks>();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsDataLoaded == false)
            {
                GetPageDataInBackground();
                IsDataLoaded = true;
            }
        }
        #region "Common Methods"

        private void GetPageDataInBackground()
        {
            BackgroundHelper bwHelper = new BackgroundHelper();
            bwHelper.AddBackgroundTask(
                                         (object s, DoWorkEventArgs a) =>
                                         {
                                             a.Result = FavoritesManager.GetFavoriteImages();
                                         },
                                               (object s, RunWorkerCompletedEventArgs a) =>
                                               {
                                                   showFavImages = (List<ShowLinks>)a.Result;
                                                   if (showFavImages.Count > 0)
                                                   {
                                                       lbximgs.ItemsSource = showFavImages;

                                                       tblkimgs.Visibility = Visibility.Collapsed;
                                                   }
                                                   else
                                                   {
                                                       tblkimgs.Text = "No images available";
                                                       tblkimgs.Visibility = Visibility.Visible;
                                                   }
                                               }
                                             );
            bwHelper.RunBackgroundWorkers();
        }

        #endregion

        private void lbximgs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                AppSettings.ShowID = (lbximgs.SelectedItem as ShowLinks).ShowID.ToString();
                AppSettings.LinkId = (lbximgs.SelectedItem as ShowLinks).LinkID.ToString();
                PageHelper.NavigateToDownloadedImagePage(AppResources.DownloadImagePageName);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbximgs_SelectionChanged event In favoriteimages file.", ex);
            }
        }

        private void btnFavSong_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OnlineVideos.View_Models.ShowDetail detailModel = new OnlineVideos.View_Models.ShowDetail();
                detailModel.AddToFavorites(lbximgs, sender as MenuItem, LinkType.Songs);
                PageHelper.NavigateTo(NavigationHelper.MainPanoramaPage);
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in btnFavSong_Click Method In Favorites file.", ex);
            }
        }
    }
}
